import Mock from "mockjs";
import { param2Obj } from "../utils/mUtils";
// 后端数据
let courseList = [];
for (let i = 0; i < 200; i++) {
  courseList.push({
    id: Mock.mock("@id"),
    src: `kecheng-picture0${(i % 8) + 1}.png`,
    title: "幼儿英语入门课程-" + i,
    type: Mock.mock("@integer(0, 1)"),
    num: Mock.mock("@integer(100, 1000)"),
    fee: Mock.mock("@integer(100, 1000)"),
    uploader: Mock.mock("@first"),
    uploadDate: Mock.mock("@date"),
  });
}
export default {
  // 查询
  getData: (config) => {
    // console.log(courseList);
    console.log(config);
    let { search, page, pageSize, startDate, endDate, typeList } = param2Obj(
      config.url
    );
    typeList = typeList.split("");
    for (let k in typeList) {
      typeList[k] = +typeList[k];
    }
    let newList = courseList.filter((elm, index) => {
      let time = new Date(elm.uploadDate).getTime();
      let flag = time >= startDate && time <= endDate;
      let high = typeList.length ? typeList.includes(elm.type) : true;
      return elm.id.indexOf(search) != -1 && flag && high;
    });
    console.log(newList);

    // 分页
    let total = newList.length;
    newList = newList.slice((page - 1) * pageSize, page * pageSize);
    return {
      code: 200,
      msg: "数据获取成功",
      data: {
        newList: newList,
        total: total,
      },
    };
  },

  // 删
  deleteData: (config) => {
    // console.log(config.body);
    let { ids } = JSON.parse(config.body);
    // console.log(ids);
    // 逐个删除
    courseList = courseList.filter((elm, index) => {
      return !ids.includes(elm.id);
    });
    return {
      code: 200,
      msg: "批量删除成功",
      data: courseList,
    };
  },

  // 改
  updateData: (config) => {
    let newData = Object.assign({}, JSON.parse(config.body));
    courseList.forEach((elm) => {
      if (elm.id == newData.id) {
        elm = Object.assign(elm, newData);
      }
    });
    return {
      code: 200,
      msg: "修改成功",
      data: null,
    };
  },
};
